﻿package 
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import com.urbansquall.echo.GroupedSound;
	import com.urbansquall.echo.SoundGroup;
	import com.urbansquall.echo.ChannelWrapper;
	import com.urbansquall.echo.Loop;
	/**
	 * ...
	 * @author Marshall Powers marshallpowers@gmail.com
	 */
	public class Main extends Sprite 
	{
		[Embed(source='../res/bgm.mp3')]
		private static var BackgroundMP3 : Class
		
		[Embed(source = '../res/Sonar.mp3')]
		private static var SonarMP3 : Class
		
		private var m_bgm : SoundGroup
		private var m_sfx : SoundGroup
		
		private var assassination : Loop
		private var ping : GroupedSound
		private var boom : GroupedSound
		private var zap : GroupedSound
		
		private var m_bgmAssassination : ChannelWrapper
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
						
			m_bgm = new SoundGroup()
			
			assassination = new Loop(m_bgm, new BackgroundMP3())
			
			m_sfx = new SoundGroup()
			
			ping = new GroupedSound(m_sfx, new SonarMP3())
			
			var muteBgm : CustomSimpleButton = new CustomSimpleButton("Toggle BGM Volume")
			addChild(muteBgm)
			muteBgm.addEventListener(MouseEvent.MOUSE_DOWN, toggleBgm)
			
			var startBgm : CustomSimpleButton = new CustomSimpleButton("Play BGM")
			addChild(startBgm)
			startBgm.x = 120
			startBgm.addEventListener(MouseEvent.MOUSE_DOWN, function (a_event : Event) : void { assassination.play() } )
			
			var stopBgm : CustomSimpleButton = new CustomSimpleButton("Stop BGM")
			addChild(stopBgm)
			stopBgm.x = 200
			stopBgm.addEventListener(MouseEvent.MOUSE_DOWN, function (a_event : Event) : void { m_bgm.stopAll() } )
			
			var crossfade : CustomSimpleButton = new CustomSimpleButton("Fade out/in")
			addChild(crossfade)
			crossfade.x = 300
			crossfade.addEventListener(MouseEvent.MOUSE_DOWN, function ( e: Event ) : void {
				Tweener.addTween(assassination, { volume : 1.0 - assassination.volume, time : 5.0, transition : "linear" })
			})
			
			var muteSfx :CustomSimpleButton = new CustomSimpleButton("Toggle SFX Volume")
			addChild(muteSfx)
			muteSfx.y = 30
			muteSfx.addEventListener(MouseEvent.MOUSE_DOWN, toggleSfx)
			
			var stopSfx : CustomSimpleButton = new CustomSimpleButton("Stop SFX Playback")
			addChild(stopSfx)
			stopSfx.x = 150
			stopSfx.y = 30
			stopSfx.addEventListener(MouseEvent.MOUSE_DOWN, function (a_event : Event) : void { m_sfx.stopAll() } )
			
			var pingVolume : TextField = new TextField()
			pingVolume.border = true
			pingVolume.type = TextFieldType.INPUT
			pingVolume.height = 20
			pingVolume.text = "1.0"			
			addChild(pingVolume)
			pingVolume.x = 100
			pingVolume.y = 60
			
			var playPing : CustomSimpleButton = sfxButton("ping", ping, pingVolume)
			addChild(playPing)
			playPing.y = 60
		}
		
		private function sfxButton(a_caption : String, a_snd : GroupedSound, a_textField : TextField) : CustomSimpleButton
		{
			var btn : CustomSimpleButton = new CustomSimpleButton(a_caption);
			btn.addEventListener(MouseEvent.MOUSE_DOWN, sfxPlayer(a_snd, a_textField));
			return btn;
		}
		
		private function sfxPlayer(a_snd : GroupedSound, a_textField : TextField) : Function
		{
			var f : Function = function (a_event : Event) : void
			{
				var wrap : ChannelWrapper = a_snd.play();
				var vol : Number = Number(a_textField.text)
				wrap.localVolume = vol
			}
			return f;
		}
		
		private function toggleBgm(a_event : Event) : void
		{
			toggleGroupVolume(m_bgm);
			
		}
		
		private function toggleSfx(a_event :Event) : void
		{
			toggleGroupVolume(m_sfx);
		}
		
		private function toggleGroupVolume(a_group : SoundGroup) : void
		{
			a_group.volume = 1.0 - a_group.volume;
		}
	}
	
}